<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/31
 * Time: 23:17
 */

namespace backend\widgets;



class UploadFile extends \yii\widgets\InputWidget
{
    /**
     * 传入的菜单数据，可以默认为空
     * @array
     */
    public $filedName = ""; //隐藏域名称
    public $type = "";
    public $imgWidth = 200; // 图片宽度
    public $imgHeight = 200; //图片高度

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
    }

    public function run()
    {
        parent::run(); // TODO: Change the autogenerated stub
        echo '<div class="company_img" data-tips="filename" ><img id="show" width="'.$this->imgWidth.'" height="'.$this->imgHeight.'" src="/assets/img/upload.png" class="businessLicense"></div>';
        echo '<input id="filename" onchange = "upload()" name="upImage" type="file" multiple="true">';
        echo '<input type="hidden" name="'.$this->filedName.'" id="hide" value=""><input type="button" value="取消缩略图" id="unsetImg" class="btn btn-danger btn-sm" style="width:'.$this->imgWidth.'px; margin-top: 4px"  >
        ';

        $js = '
        $("#filename").hide();
        $(\'#unsetImg\').on(\'click\',function(){
            $(\'#hide\').val("");
            $(\'#show\').attr("src","/assets/img/upload.png");
        });
        $(\'.company_img\').on(\'click\',function(){
                    var id = $(this).attr(\'data-tips\');
                    $(\'#\'+id).trigger(\'click\');
	            });
	            ';
        echo "<script>";
        echo 'function upload(){';
        echo ' $.ajaxFileUpload({';
        echo 'type:\'POST\',';
        echo 'url:\'/upload/qiniu.html\',';
        echo 'secureuri:false,';
        echo 'fileElementId:"filename",';
        echo 'dataType: \'json\',';
        echo ' success: function (data){';
        echo ' if(parseInt(data.status) == 1){';
        echo '$("#show").attr("src",data.data);';
        echo '$("#hide").val(data.data);';
        echo '}else{';
        echo 'App.alert({
                     type: \'danger\', // alert\'s type
                     message: data.message, // alert\'s message
                     icon: \'fa fa-warning\' // put icon class before the message
                     });';
        echo '}},';
        echo ' error: function(){ App.alert({
                     type: \'danger\', // alert\'s type
                     message: "网络超时", // alert\'s message
                     icon: \'fa fa-warning\' // put icon class before the message
                     }); } }) };';
        echo "</script>";
        $this->getView()->registerJs($js);
    }
}